const express = require('express')
const router = express.Router()
const conn = require('../util/sql.js')
const jwt = require('jsonwebtoken')
router.use(express.urlencoded())
// 文章搜索
router.get('/search', (req, res) => {
    console.log('文章搜索接收参数', req.query);
    const {
        id,
        name
    } = req.query
    const sqlStr = `select * from articles where id="${id}" or name="${name}"`
    conn.query(sqlStr, (err, result) => {
        if (err) {
            res.json({
                "status": 0,
                "message": "文章搜索失败！"
            })
            return
        }
        res.json({
            "status": 0,
            "message": "文章搜索成功！",
            data: result,
        })
    })
    res.send('ok')
})
// 获取文章类型 ok
router.get('/category', (req, res) => {
    console.log('文章类型接收参数', req.query);
    const {
        id,
        name
    } = req.query
    const sqlStr = `select * from categories where id="${id}" and name="${name}"`
    conn.query(sqlStr, (err, result) => {
        if (err) {
            res.json({
                "status": 0,
                "message": "文章类型获取失败！"
            })
            return
        }
        res.json({
            "status": 0,
            "message": "文章类型获取成功！",
            data: result[0],
        })
    })
    // res.send('ok')
})
// 热点图
router.get('/hotpic', (req, res) => {
    console.log('获取热点图参数', req.query);
    const {
        id,
        cover
    } = req.query
    const sqlStr = `select * from articles where id="${id}" limit 5`
    conn.query(sqlStr, (err, result) => {
        if (err) {
            res.json({
                "status": 0,
                "message": "热点图获取失败！"
            })
            return
        }
        res.json({
            "status": 0,
            "message": "热点图获取成功！",
            data: result[0],
        })
    })
    // res.send('ok')
})
// 文章热门排行 ok
router.get('/rank', (req, res) => {
    console.log('文章热门排行参数', req.query);
    const {id,title} = req.query
    const sqlStr = `select * from articles where id="${id}" and title="${title}" limit 7`
    conn.query(sqlStr,(err,result)=>{
        if (err) {
            res.json({
                "status": 0,
                "message": "文章热门排行获取失败！"
            })
            return
        }
        console.log(result);
        res.json({
            status: 0,
            message: "文章热门排行获取成功！",
            data:result,
        })
    })
})
// 最新资讯 ok
router.get('/latest', (req, res) => {
    console.log('最新资讯参数', req.query);
    // const {id} = req.query
    const sqlStr = `select * from articles`
    conn.query(sqlStr,(err,result)=>{
        if (err) {
            res.json({
                "status": 0,
                "message": "最新资讯获取失败！"
            })
            return
        }
        console.log(result);
        res.json({
            status: 0,
            message: "最新资讯获取成功！",
            data:result,
        })
    })
})
// 最新评论
router.get('/latest_comment', (req, res) => {
    console.log('最新评论参数', req.query);
    const {author,date,intro} = req.query
    const sqlStr = `select * from comments where author="${author}" and date="${date}"`
    conn.query(sqlStr,(err,result)=>{
        if (err) {
            res.json({
                "status": 0,
                "message": "最新评论获取失败！"
            })
            return
        }
        console.log(result);
        res.json({
            status: 0,
            message: "最新评论获取成功！",
            data:result[author,date,intro],
        })
    })
})
// 文章详细内容
router.get('/artitle', (req, res) => {
    console.log('文章详细内容参数', req.query);
    const {id} = req.query
    const sqlStr = `select * from articles where id="${id}"`
    conn.query(sqlStr,(err,result)=>{
        if (err) {
            res.json({
                "status": 0,
                "message": "文章详细内容获取失败！"
            })
            return
        }
        console.log(result);
        res.json({
            status: 0,
            message: "文章详细内容获取成功！",
            data:result,
        })
    })
})

module.exports = router